package com.cubeofnine.sugar.activity.export_contact;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.ContentResolver;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TableLayout.LayoutParams;

import com.cubeofnine.android.api.ContactAPI;
import com.cubeofnine.android.api.contact.Address;
import com.cubeofnine.android.api.contact.Email;
import com.cubeofnine.android.api.contact.Organization;
import com.cubeofnine.android.api.contact.Phone;
import com.cubeofnine.sugar.ActivityImpt;
import com.cubeofnine.sugar.R;
import com.cubeofnine.utils.ScrollingTextView;

public class PhoneContactDetailActivity extends ActivityImpt {

	private CheckBox checkAll = null;
	private ImageView syncBtn = null;
	private TextView headerText = null;

	public String getClassName() {
		return PhoneContactDetailActivity.class.getName();
	}

	public void onCreate(Bundle savedInstanceState) {
		Log.i(getClassName(), "onCreate() : Start");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.phone_contacts);

		headerText = (TextView) findViewById(R.id.header_title);
		checkAll = (CheckBox) findViewById(R.id.check_all);
		checkAll.setVisibility(CheckBox.GONE);
		syncBtn = (ImageView) findViewById(R.id.sync_btn);
		syncBtn.setVisibility(ImageView.GONE);

		String contactId = null;
		String contactName = null;
		try {
			contactId = getIntentParams().getString("CONTACT_ID");
			contactName = getIntentParams().getString("CONTACT_NAME");

			headerText.setText(contactName);

			initialProcess(contactId, contactName);
		} catch (Exception e) {
			alertMessage("Read Contacts Error.", true);
		}

		Log.i(getClassName(), "onCreate() : End");
	}

	public void initialProcess(String contactId, String contactName) {
		ContentResolver cr = getContentResolver();
		ContactAPI api = new ContactAPI();
		api.setCr(cr);

		ArrayList<Phone> phones = null;
		Iterator<Phone> phoneIter = null;
		Phone phone = null;
		String phoneNumber = null;
		String phoneType = null;
		String phoneLabel = null;

		ArrayList<Email> emails = null;
		Iterator<Email> emailIter = null;
		Email email = null;
		String emailAddress = null;
		String emailType = null;
		String emailLabel = null;

		ArrayList<Address> addresses = null;
		Iterator<Address> addressIter = null;
		Address address = null;
		String fullAddress = null;
		String addressLabel = null;

		ArrayList<Organization> organizations = null;
		Iterator<Organization> organizationIter = null;
		Organization organization = null;
		String companyName = null;
		String jobTitle = null;
		String organizationType = null;
		String organizationLabel = null;

		LinearLayout root = (LinearLayout) findViewById(R.id.body_root_linear_layout);
		root.removeAllViewsInLayout();

		LinearLayout layout = null;
		ScrollingTextView textView = null;

		// Contact Root Layout
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);// W,H
		layoutParams.setMargins(0, 2, 0, 2);// left, top, right, bottom

		LinearLayout.LayoutParams labelParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);// W,H
		labelParams.setMargins(20, 2, 0, 2);// left, top, right, bottom

		LinearLayout.LayoutParams valueParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);// W,H
		valueParams.setMargins(40, 0, 0, 0);// left, top, right, bottom

		phones = api.getPhoneNumbers(contactId);
		if (phones != null && !phones.isEmpty()) {
			layout = new LinearLayout(this);
			layout.setLayoutParams(layoutParams);
			layout.setOrientation(LinearLayout.HORIZONTAL);
			layout.setBackgroundColor(Color.rgb(0, 102, 255));// Blue

			textView = new ScrollingTextView(this);
			textView.setLayoutParams(labelParams);
			textView.setText("Phone");
			textView.setTextColor(Color.WHITE);
			textView.setTextAppearance(this, android.R.attr.textAppearanceLarge);
			textView.setSingleLine(true);
			textView.setHorizontallyScrolling(true);
			textView.setEllipsize(TruncateAt.MARQUEE);
			textView.setMarqueeRepeatLimit(1);
			textView.setMovementMethod(new ScrollingMovementMethod());
			layout.addView(textView);
			root.addView(layout);

			phoneIter = phones.iterator();
			while (phoneIter.hasNext()) {
				phone = phoneIter.next();
				phoneNumber = phone.getNumber();
				phoneType = phone.getType();
				phoneLabel = phone.getLabel();

				Log.d(getClassName(), "phoneNumber = " + phoneNumber);
				Log.d(getClassName(), "phoneType = " + phoneType);
				Log.d(getClassName(), "phoneLabel = " + phoneLabel);

				layout = new LinearLayout(this);
				layout.setLayoutParams(layoutParams);
				layout.setOrientation(LinearLayout.HORIZONTAL);

				textView = new ScrollingTextView(this);
				textView.setLayoutParams(valueParams);
				textView.setText(phoneLabel);
				textView.setTextColor(Color.GRAY);
				textView.setTextAppearance(this,
						android.R.attr.textAppearanceSmall);
				textView.setSingleLine(true);
				textView.setHorizontallyScrolling(true);
				textView.setEllipsize(TruncateAt.MARQUEE);
				textView.setMarqueeRepeatLimit(1);
				textView.setMovementMethod(new ScrollingMovementMethod());
				layout.addView(textView);
				root.addView(layout);

				layout = new LinearLayout(this);
				layout.setLayoutParams(layoutParams);
				layout.setOrientation(LinearLayout.HORIZONTAL);

				textView = new ScrollingTextView(this);
				textView.setLayoutParams(valueParams);
				textView.setText(phoneNumber);
				textView.setTextColor(Color.rgb(0, 102, 255));// Blue
				textView.setTextAppearance(this,
						android.R.attr.textAppearanceLarge);
				textView.setSingleLine(true);
				textView.setHorizontallyScrolling(true);
				textView.setEllipsize(TruncateAt.MARQUEE);
				textView.setMarqueeRepeatLimit(1);
				textView.setMovementMethod(new ScrollingMovementMethod());
				layout.addView(textView);
				root.addView(layout);
			}
		}

		emails = api.getEmailAddresses(contactId);
		if (emails != null && !emails.isEmpty()) {
			layout = new LinearLayout(this);
			layout.setLayoutParams(layoutParams);
			layout.setOrientation(LinearLayout.HORIZONTAL);
			layout.setBackgroundColor(Color.rgb(0, 102, 255));// Blue

			textView = new ScrollingTextView(this);
			textView.setLayoutParams(labelParams);
			textView.setText("Email");
			textView.setTextColor(Color.WHITE);
			textView.setTextAppearance(this, android.R.attr.textAppearanceLarge);
			textView.setSingleLine(true);
			textView.setHorizontallyScrolling(true);
			textView.setEllipsize(TruncateAt.MARQUEE);
			textView.setMarqueeRepeatLimit(1);
			textView.setMovementMethod(new ScrollingMovementMethod());
			layout.addView(textView);
			root.addView(layout);

			emailIter = emails.iterator();
			while (emailIter.hasNext()) {
				email = emailIter.next();
				emailAddress = email.getAddress();
				emailType = email.getType();
				emailLabel = email.getLabel();

				Log.d(getClassName(), "emailAddress = " + emailAddress);
				Log.d(getClassName(), "emailType = " + emailType);
				Log.d(getClassName(), "emailLabel = " + emailLabel);

				layout = new LinearLayout(this);
				layout.setLayoutParams(layoutParams);
				layout.setOrientation(LinearLayout.HORIZONTAL);

				textView = new ScrollingTextView(this);
				textView.setLayoutParams(valueParams);
				textView.setText(emailLabel);
				textView.setTextColor(Color.GRAY);
				textView.setTextAppearance(this,
						android.R.attr.textAppearanceSmall);
				textView.setSingleLine(true);
				textView.setHorizontallyScrolling(true);
				textView.setEllipsize(TruncateAt.MARQUEE);
				textView.setMarqueeRepeatLimit(1);
				textView.setMovementMethod(new ScrollingMovementMethod());
				layout.addView(textView);
				root.addView(layout);

				layout = new LinearLayout(this);
				layout.setLayoutParams(layoutParams);
				layout.setOrientation(LinearLayout.HORIZONTAL);

				textView = new ScrollingTextView(this);
				textView.setLayoutParams(valueParams);
				textView.setText(emailAddress);
				textView.setTextColor(Color.rgb(0, 102, 255));// Blue
				textView.setTextAppearance(this,
						android.R.attr.textAppearanceLarge);
				textView.setSingleLine(true);
				textView.setHorizontallyScrolling(true);
				textView.setEllipsize(TruncateAt.MARQUEE);
				textView.setMarqueeRepeatLimit(1);
				textView.setMovementMethod(new ScrollingMovementMethod());
				layout.addView(textView);
				root.addView(layout);
			}
		}

		addresses = api.getContactAddresses(contactId);
		if (addresses != null && !addresses.isEmpty()) {
			layout = new LinearLayout(this);
			layout.setLayoutParams(layoutParams);
			layout.setOrientation(LinearLayout.HORIZONTAL);
			layout.setBackgroundColor(Color.rgb(0, 102, 255));// Blue

			textView = new ScrollingTextView(this);
			textView.setLayoutParams(labelParams);
			textView.setText("Address");
			textView.setTextColor(Color.WHITE);
			textView.setTextAppearance(this, android.R.attr.textAppearanceLarge);
			textView.setSingleLine(true);
			textView.setHorizontallyScrolling(true);
			textView.setEllipsize(TruncateAt.MARQUEE);
			textView.setMarqueeRepeatLimit(1);
			textView.setMovementMethod(new ScrollingMovementMethod());
			layout.addView(textView);
			root.addView(layout);

			addressIter = addresses.iterator();
			while (addressIter.hasNext()) {
				address = addressIter.next();
				fullAddress = address.toString();
				addressLabel = address.getLabel();

				Log.d(getClassName(), "fullAddress = " + fullAddress);
				Log.d(getClassName(), "addressLabel = " + addressLabel);

				layout = new LinearLayout(this);
				layout.setLayoutParams(layoutParams);
				layout.setOrientation(LinearLayout.HORIZONTAL);

				textView = new ScrollingTextView(this);
				textView.setLayoutParams(valueParams);
				textView.setText(addressLabel);
				textView.setTextColor(Color.GRAY);
				textView.setTextAppearance(this,
						android.R.attr.textAppearanceSmall);
				textView.setSingleLine(true);
				textView.setHorizontallyScrolling(true);
				textView.setEllipsize(TruncateAt.MARQUEE);
				textView.setMarqueeRepeatLimit(1);
				textView.setMovementMethod(new ScrollingMovementMethod());
				layout.addView(textView);
				root.addView(layout);

				layout = new LinearLayout(this);
				layout.setLayoutParams(layoutParams);
				layout.setOrientation(LinearLayout.HORIZONTAL);

				textView = new ScrollingTextView(this);
				textView.setLayoutParams(valueParams);
				textView.setText(fullAddress);
				textView.setTextColor(Color.rgb(0, 102, 255));// Blue
				textView.setTextAppearance(this,
						android.R.attr.textAppearanceLarge);
				textView.setSingleLine(true);
				textView.setHorizontallyScrolling(true);
				textView.setEllipsize(TruncateAt.MARQUEE);
				textView.setMarqueeRepeatLimit(1);
				textView.setMovementMethod(new ScrollingMovementMethod());
				layout.addView(textView);
				root.addView(layout);
			}
		}

		organizations = api.getContactOrg(contactId);
		if (organizations != null && !organizations.isEmpty()) {
			layout = new LinearLayout(this);
			layout.setLayoutParams(layoutParams);
			layout.setOrientation(LinearLayout.HORIZONTAL);
			layout.setBackgroundColor(Color.rgb(0, 102, 255));// Blue

			textView = new ScrollingTextView(this);
			textView.setLayoutParams(labelParams);
			textView.setText("Organization");
			textView.setTextColor(Color.WHITE);
			textView.setTextAppearance(this, android.R.attr.textAppearanceLarge);
			textView.setSingleLine(true);
			textView.setHorizontallyScrolling(true);
			textView.setEllipsize(TruncateAt.MARQUEE);
			textView.setMarqueeRepeatLimit(1);
			textView.setMovementMethod(new ScrollingMovementMethod());
			layout.addView(textView);
			root.addView(layout);

			organizationIter = organizations.iterator();
			while (organizationIter.hasNext()) {
				organization = organizationIter.next();
				companyName = organization.getOrganization();
				jobTitle = organization.getTitle();
				organizationType = organization.getType();
				organizationLabel = organization.getLabel();

				Log.d(getClassName(), "companyName = " + companyName);
				Log.d(getClassName(), "jobTitle = " + jobTitle);
				Log.d(getClassName(), "organizationType = " + organizationType);
				Log.d(getClassName(), "organizationLabel = "
						+ organizationLabel);

				layout = new LinearLayout(this);
				layout.setLayoutParams(layoutParams);
				layout.setOrientation(LinearLayout.HORIZONTAL);

				textView = new ScrollingTextView(this);
				textView.setLayoutParams(valueParams);
				textView.setText(organizationLabel);
				textView.setTextColor(Color.GRAY);
				textView.setTextAppearance(this,
						android.R.attr.textAppearanceSmall);
				textView.setSingleLine(true);
				textView.setHorizontallyScrolling(true);
				textView.setEllipsize(TruncateAt.MARQUEE);
				textView.setMarqueeRepeatLimit(1);
				textView.setMovementMethod(new ScrollingMovementMethod());
				layout.addView(textView);
				root.addView(layout);

				layout = new LinearLayout(this);
				layout.setLayoutParams(layoutParams);
				layout.setOrientation(LinearLayout.HORIZONTAL);

				textView = new ScrollingTextView(this);
				textView.setLayoutParams(valueParams);
				textView.setText(companyName);
				textView.setTextColor(Color.rgb(0, 102, 255));// Blue
				textView.setTextAppearance(this,
						android.R.attr.textAppearanceLarge);
				textView.setSingleLine(true);
				textView.setHorizontallyScrolling(true);
				textView.setEllipsize(TruncateAt.MARQUEE);
				textView.setMarqueeRepeatLimit(1);
				textView.setMovementMethod(new ScrollingMovementMethod());
				layout.addView(textView);
				root.addView(layout);

				layout = new LinearLayout(this);
				layout.setLayoutParams(layoutParams);
				layout.setOrientation(LinearLayout.HORIZONTAL);

				textView = new ScrollingTextView(this);
				textView.setLayoutParams(valueParams);
				textView.setText(jobTitle);
				textView.setTextColor(Color.rgb(0, 102, 255));// Blue
				textView.setTextAppearance(this,
						android.R.attr.textAppearanceLarge);
				textView.setSingleLine(true);
				textView.setHorizontallyScrolling(true);
				textView.setEllipsize(TruncateAt.MARQUEE);
				textView.setMarqueeRepeatLimit(1);
				textView.setMovementMethod(new ScrollingMovementMethod());
				layout.addView(textView);
				root.addView(layout);
			}
		}
	}

	public void onClickHomeButton(View view) {
		try {
			view.startAnimation(AnimationUtils.loadAnimation(this,
					R.anim.image_click));
			createMenuPopup(R.string.label_home_menu,
					new String[] { MENU_LOGOUT });
		} catch (Exception e) {
			Log.e(ActivityImpt.class.getName(), "Error", e);
		}
	}
}
